<template>
	<view class="login">
		<view class="head">
			<image src="" mode=""></image>
		</view>
		<view class="bottom">
			<view>应用需要获取你账户信息和头像</view>
			<button class="btn" type="primary" open-type="getUserInfo" @click="authorization">授权</button>
			<view>推荐人ID：15806623</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			};
		},
		methods: {
			authorization(){
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						uni.getUserInfo({
							provider: 'weixin',
							success: function(infoRes) {
								var params = {
									encryptedData: infoRes.encryptedData,
									iv: infoRes.iv,
									wxCode: loginRes.code,
									type: 1
								}
								uni.request({
									url: 'http://10.10.10.22:8088/login', //仅为示例，并非真实接口地址。
									method:'post',
									data: params,
									header:{
										'Content-Type': 'application/x-www-form-urlencoded',
									},
									success: (res) => {
										if(res.data.errorNo == 0){
											//登录成功,存储cookie
											let result = res.data.results;									
											uni.setStorage({
												key: 'token',
												data: result.token
											});
											uni.setStorage({
												key: 'accountId',
												data: result.data.accountId,
											});
											uni.setStorage({
												key: 'accountName',
												data: result.data.accountName
											});
											uni.setStorage({
												key: 'headImage',
												data: result.data.headImage
											});
											if(!result.data.phone){
												uni.redirectTo({
														url: '/pages/login/binding/binding?recommenderAccountId='+(result.data.recommenderAccountId?result.data.recommenderAccountId:'')
												});
												return false;
											}
											if(!result.data.recommenderAccountId){
												uni.redirectTo({
														url: '/pages/login/relation/relation'
												});
												return false;
											}
											uni.redirectTo({
													url: '/pages/index/index/index'
											});
										}
									}
								});
							}
						});
					}
				});
			},
		}
	}
</script>

<style>
.login {
	width: 100%;
	min-height: 100%;
	background-color: #f3f3f3;
	position: absolute;
	font-size: 24upx;
}
.login .head {
	margin-top: 180upx;
	text-align: center;
}
.login .head image {
	width: 200upx;
	height: 200upx;
	background-color: #fff;
	border-radius: 36upx;
}
.login .bottom {
	width: 100%;
	position: absolute;
	bottom: 0;
	left: 0;
	text-align: center;
	padding-bottom: 30upx;
}

.login .bottom .btn {
	width: 358upx;
	height: 105upx;
	background-color: #606060;
	margin-top: 50upx;
	margin-bottom: 160upx;
	font-size: 30upx;
	line-height: 105upx;
}
</style>
